package com.example.test_1_21.demo;

import com.example.test_1_21.common.Consumer;
import com.example.test_1_21.common.MQException;
import com.example.test_1_21.mqclient.Channel;
import com.example.test_1_21.mqclient.Connection;
import com.example.test_1_21.mqclient.ConnectionFactory;
import com.example.test_1_21.mqserver.core.BasicProperties;
import com.example.test_1_21.mqserver.core.ExchangeType;

import java.io.IOException;

//这个类表示一个消费者，通产这个类是在一个独立的服务器中
public class DemoConsumer {
    public static void main(String[] args) throws IOException, MQException, InterruptedException {
        System.out.println("启动消费者");
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setPort(9090);

        Connection connection=factory.newConnection();
        Channel channel=connection.createChannel();

        channel.exchangeDeclare("testExchange", ExchangeType.DIRECT,true,false,null);
        channel.queueDeclare("testQueue",true,false,false,null);

        channel.basicConsume("testQueue", true, new Consumer() {
            @Override
            public void handleDelivery(String consumerTag, BasicProperties basicProperties, byte[] body) throws MQException, IOException {
                System.out.println("[消费数据] 开始！");
                System.out.println("consumerTag="+consumerTag);
                System.out.println("basicProperties="+basicProperties);
                String bodyString=new String(body,0,body.length);
                System.out.println("body="+bodyString);
                System.out.println("[消费数据] 结束！");
            }
        });

        while (true){
            Thread.sleep(500);
        }
    }
}
